﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace SimplexEngine
{
    public class AnimationMeshEntity : Entity
    {
        public AnimationMeshEntity(String scene_object_name, String mesh_name)
            : base(scene_object_name)
        {
            Resource mesh = Root.Instance.ContentSystem.GetResource(mesh_name);
            animation_mesh_instance = AnimationMesh.CreateAnimationMeshInstance(mesh);
        }

        protected override void OnDraw(GameTime game_time, Int64 frame_number)
        {
            List<RenderElement> render_elements = animation_mesh_instance.CreateRenderElements(this);

            foreach (RenderElement render_element in render_elements)
                Root.Instance.RenderSystem.AddToRenderList(render_element);

        }

        public virtual String ActiveAnimation
        {
            get { return animation_mesh_instance.ActiveAnimation; }
            set { animation_mesh_instance.ActiveAnimation = value; }
        }

        public virtual float CurrentTime { get { return animation_mesh_instance.CurrentTime; } set { animation_mesh_instance.CurrentTime = value; } }

        public virtual AnimationLoopMode LoopMode { get { return animation_mesh_instance.LoopMode; } set { animation_mesh_instance.LoopMode = value; } } 

        protected override void OnUpdate(GameTime game_time, Int64 frame_number)
        {
            animation_mesh_instance.OnUpdate(game_time, frame_number);
            base.OnUpdate(game_time, frame_number);
        }

        public virtual void SetMaterialToAllPart(Material material, Boolean clone, Boolean copy_old_parameters)
        {
            animation_mesh_instance.SetMaterialToAllPart(material, clone, copy_old_parameters);
        }

        public float Speed { get { return animation_mesh_instance.Speed; } set { animation_mesh_instance.Speed = value; } }

        protected AnimationMeshInstance animation_mesh_instance = null;
    }

    public class AnimationMeshEntityFactory : SceneObjectFactory
    {
        public override string Type
        {
            get { return "ANIMATIONMESHENTITY"; }
        }

        public override SceneObject CreateSceneObject(String name, Dictionary<String, String> parameters)
        {
            return new AnimationMeshEntity(name, parameters["MeshName"]);
        }
    }
}
